#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project : bigDataProject 
@File    : pay_api.py
@IDE     : PyCharm 
@Author  : Mario
@Date    : 2022/11/14 10:16 
@Describe: POS支付相关接口
"""
import time
from typing import Text, Union

from api.pos_api.base_pos_api import BasePosAPI
from utils.common.common_control import get_json_result


class PayAPI(BasePosAPI):

    def pay_type_list(self):
        """
        获取门店支付方式
        :return:
        """
        payload = '{}'
        response = self.http_request('pay/type/list', payload, "POS获取门店支付方式")
        return response

    def order_pay_detail(self, order_no: Text):
        """
        订单支付详情
        :param order_no: 订单号
        :return:
        """
        payload = '{"orderNo": "' + order_no + '"}'
        response = self.http_request('pay/common/order-pay/detail', payload, "POS订单支付详情")
        return response

    def cash_pay(self, order_no: Text, pay_amount: Union[float, Text], pay_mode_id: Text):
        """
        现金支付
        :param order_no:订单号
        :param pay_amount:支付金额
        :param pay_mode_id:支付方式
        :return:
        """
        payload = '{"orderNo":"' + order_no + '","payAmount":"' + str(
            pay_amount) + '","payModeId":"' + pay_mode_id + '"}'
        response = self.http_request('pay/cash/pay', payload, "POS现金支付")
        return response

    def confirm_checkout(self, order_no: Text):
        """
        确认结台
        :param order_no: 订单号
        :return:
        """
        time.sleep(1)
        payload = '{"orderNo":"' + order_no + '"}'
        response = self.http_request('order/settlement/confirm-checkout', payload, "POS确认结台")
        return response

    def pay_common_refund(self, order_no: Text, refund_amount: Text, pay_no: Text):
        """
        支付退款
        :param order_no: 订单号
        :param refund_amount: 退款金额
        :param pay_no: 支付号(商户订单号)
        :return:
        """
        payload = '{"orderNo":"' + order_no + '","refundAmount":"' + refund_amount + '","payNo":"' + pay_no + '"}'
        response = self.http_request('pay/common/refund', payload, "POS支付退款")
        return response

    def pay_xinmeida_cancel(self, order_no: Text, pay_no: Text):
        """
        取消新美大支付
        :param order_no: 订单号
        :param pay_no: 支付号(商户订单号)
        :return:
        """
        payload = '{"orderNo":"' + order_no + '","payNo":"' + pay_no + '"}'
        response = self.http_request('pay/xinmeida/cancel', payload, "POS取消新美大支付")
        return response

    def order_pay(self, order_no: Text):
        """
        订单结账
        :param order_no: 订单号
        :return:
        """
        payload = '{"orderNo":"' + order_no + '"}'
        response = self.http_request('order/pay', payload, "POS订单结账")
        return response

    def get_pay_mode_id(self, pay_name: Text = "现金支付") -> Text:
        """
        获取支付方式ID
        :param pay_name: 支付名称，默认获取【现金支付】方式
        :return:
        """
        pay_type_list = get_json_result(self.pay_type_list(),
                                        "$.data.payTypeList[?(@.typeName == '{}')]".format(pay_name))
        pay_mode_id = str(pay_type_list['payModeList'][0]['payModeId'])
        return pay_mode_id


if __name__ == '__main__':
    test = PayAPI('823882', 'sit')
    order_info = test.order_pay_detail("1010000287859")
    print(get_json_result(order_info, "$..payApiResponseList"))
    print(type(get_json_result(order_info, "$..payApiResponseList")))
